﻿using System;
using System.Collections.Generic;
using System.ComponentModel.Composition.Hosting;
using System.Linq;
using System.Web;
using System.Web.Http;
using System.Web.Http.OData.Builder;
using WooCoo.DataPortal.WebHost.Route;
using WooCoo.Portal.Contracts;

namespace WooCoo.DataPortal.WebApiHost
{
    public class WebApiConfig
    {
        public static void Register(HttpConfiguration config)
        {
            // Web API configuration and services

            // Web API routes
            //config.MapHttpAttributeRoutes();

            //config.Routes.MapHttpRoute(
            //    name: "DefaultApi",
            //    routeTemplate: "{controller}/{id}",
            //    defaults: new { id = RouteParameter.Optional }
            //);
            ODataConventionModelBuilder builder = new ODataConventionModelBuilder();
            builder.EntitySet<SysUser>("SysUser");
            builder.EntitySet<SysResource>("SysResource");
            //builder.AddEntitySet()
            config.Routes.MapDataPotalRoute("ODataPortal", "ODataPortal", builder.GetEdmModel());

            //WooCoo.RuntimeEnvironment.DbContextType = typeof(WooCooTest.Data.WooCooTestContainer);
            WooCoo.RuntimeEnvironment.DefaultEntityRepositoryType = typeof(WooCoo.Data.EF.DefaultEntityRepository<>);
            //IOC初始化
            AggregateCatalog catalog = new AggregateCatalog();
            catalog.Catalogs.Add(new DirectoryCatalog(System.IO.Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "bin"),"WooCoo*.dll"));
            WooCoo.IoC.Container.Current = new WooCoo.IoC.MEFAdapter(new Lazy<CompositionContainer>(() => new CompositionContainer(catalog)));
        }
    }
}